package com.hrhr74.penguinstream.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hrhr74.penguinstream.domain.dto.UserDTO;
import com.hrhr74.penguinstream.domain.dto.UserEditDTO;
import com.hrhr74.penguinstream.domain.login.UserLogin;
import com.hrhr74.penguinstream.domain.query.UserPageQuery;
import com.hrhr74.penguinstream.domain.vo.UserVO;
import com.hrhr74.penguinstream.service.UserEditFactory;
import com.hrhr74.penguinstream.service.IUserService;
import com.hrhr74.penguinstream.service.LoginContext;
import com.hrhr74.penguinstream.service.UserEdit;
import com.hrhr74.penguinstream.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    private final LoginContext loginContext;

    private final UserEditFactory userEditFactory;
    /**
     * 注册/创建用户
     * @param userDTO
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody UserDTO userDTO){
        return userService.register(userDTO);
    }

    /**
     * 登录
     * @param userLogin
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody UserLogin  userLogin){
        return loginContext.userLogin(userLogin);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("send")
    public Result sendCode(@RequestParam("phone") String phone){
        return userService.sendCode(phone);
    }

    /**
     * 根据id查询用户
     * @return
     */
    @PostMapping("{id}")
    public Result<UserVO> getUserById(@PathVariable("id") Long id){
        return Result.ok(BeanUtil.copyProperties(userService.getById(id),UserVO.class),"查询成功");
    }

    /**
     * 用户修改
     * @param userEditDTO
     * @return
     */
    @PutMapping("edit")
    public Result editUser(@RequestBody UserEditDTO userEditDTO,@RequestParam String type){
        UserEdit userEdit = userEditFactory.getUserEdit(type);

        return userEdit.editUser(userEditDTO);
    }

    @PostMapping("list")
    public Result<List<UserPageQuery>> pageQueryUser(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,@RequestBody UserDTO userDTO){
        return userService.pageQueryUser(pageNo,pageSize,userDTO);
    }
}
